<?php
/**
 * 购物车控制器
 */
 class CarController extends CommonController{
 	//购物车首页
 	public function index(){
 		if(empty($_SESSION['cart']['goods'])){
// 			$this->error('购物车访问错误');
 		}
 		$cartGoods = Cart::getAllData();
		if(!empty($cartGoods)){
				$this->assign('cartGoods',$cartGoods);
				$goodsKey = array_keys($cartGoods['goods']);
				$this->assign('goodsKey',$goodsKey);
		}

 		$this->dis('mycar');
 	}
	
	//删除购物车商品
	public function delCarGoods(){
		$key = $_POST['key'];
//		p($_SESSION['cart']['goods'][$key]['total']);die;
		//删除钱的单位数
		$oldeNum = count($_SESSION['cart']['goods']);
		//赋值新总价格
		$_SESSION['cart']['total'] = (int)$_SESSION['cart']['total'] - (int)$_SESSION['cart']['goods'][$key]['total'];
		//删除商品
		unset($_SESSION['cart']['goods'][$_POST['key']]);
		//删除后的单位数
		$newNum = count($_SESSION['cart']['goods']);
		//赋值新件数
		$_SESSION['cart']['total_rows'] = $newNum;
		//判断是否删除
		if($oldeNum > $newNum){
			$this->ajax('1');
		}else{
			$this->ajax('0');
		}
	}
	
	//清空购物车
	public function clearCar(){
		if(IS_AJAX){
			unset($_SESSION['cart']);
			$this->ajax('1');
		}
	}
	
	//删除选中商品
	public function clearChooseGoods(){
		if(IS_AJAX){
			$goodsKey = trim($_POST['goodsKey'],',');
			$goodsKey = explode(',', $goodsKey);
			foreach($goodsKey as $key => $v){
				unset($_SESSION['cart']['goods'][$v]);
			}
			$this->ajax('1');
		}
	}
	
	//确认购物车
	public function sureCarGoods(){
		if(IS_AJAX){
			//获取post商品键名
			$goodsKey = trim($_POST['attr'],',');
			//键名转数组
			$goodsKey = explode(',', $goodsKey);
			//商品件数
			$goodsNums= trim(Q('post.goodsNums'),',');
			$goodsNums= explode(',',$goodsNums );
			$total = 0;
			$num = 0;
			//重组数组 将post的键名 所在数组值取出 
			foreach($goodsKey as $key => $v){				
				$data['cart']['goods'][$v] = $_SESSION['cart']['goods'][$v];
				$data['cart']['goods'][$v]['num'] = $goodsNums[$key];	
				(int)$total += (INT)$data['cart']['goods'][$v]['num'] * (INT)$data['cart']['goods'][$v]['price'] ;	
				(int)$num +=  (INT)$data['cart']['goods'][$v]['num'];	
			}
			//计算商品件数
			$data['cart']['total_rows'] = $num;
			//计算商品总价
			$data['cart']['total'] = $total;
			//删除session里商品信息
			unset($_SESSION['cart']['goods']);
			//将组合的商品信息赋值回去
			$_SESSION['cart']['goods'] = $data['cart']['goods'];
			//返回异步信息
			$this->ajax('1');
		}
	} 
	
	//确认订单
	public function mycarStep_2(){
		if(IS_POST){
		}
		$uid = $_SESSION['uid'];
		//分配购物车信息
 		if(empty($_SESSION['cart']['goods'])){
// 			$this->error('购物车访问错误');
 		}
 		$cartGoods = Cart::getAllData();
		if(!empty($cartGoods)){
				$this->assign('cartGoods',$cartGoods);
				$goodsKey = array_keys($cartGoods['goods']);
				$this->assign('goodsKey',$goodsKey);
		}
		//查询分配用户地址信息
		$address = M("user_address")->where("user_uid ='{$uid}'")->all();
		$this->assign('address',$address);
		$this->dis('mycarStep_2');
	}
	
	//确认订单 支付
	public function mycarStep_3(){
		//查询订单信息  分配视图 读数据库
		$oid = Q('get.oid');
		//获取订单号对应的所有商品
		$oidInfo = M('order_list')->where("order_id='{$oid}'")->all();
		//定义初始值
		$price = 0;
		foreach($oidInfo as $k =>$v){
			$price += $v['order_list_subtotal'];//总价格
		}
		//读取用户moeny
		$uid= $_SESSION['uid'];
		$userInfo = M('user')->where("uid='{$uid}'")->find();
		//组合支付信息
		$data['total'] = $price;//总价
		$data['price'] = $price;//需要支付金额
		$data['postage'] = 0;//邮费
		//低于69支付邮费10
		if($data['total']<69){
			$data['price'] = $data['total'] + 10;
			$data['postage'] = 10;
		}		
		//组合支付前后用户金额
		$data['befor'] = $userInfo['money'];
		$data['after'] = $userInfo['money'] - $data['price'];

		$this->assign('data',$data);
		$this->dis('mycarStep_3');
	}
	
	//支付成功
	public function mycarStep_4(){
		$uid = $_SESSION['uid'];
		$orderId = $_SESSION['orderId'];
		//查询订单支付方式 执行对应方法
		$orderInfo = M('user_order')->where("oid='{$orderId}' and user_uid ='{$uid}'")->find();
		//查询用户余额
		$userInfo = M('user')->where("uid = '{$uid}'")->find();
		$this->assign('userInfo',$userInfo);
		$this->assign('orderInfo',$orderInfo);
		$this->dis('mycarStep_4');
	}
	
	//保存地址
	public function saveaddress(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			//组合用户地址信息 
			$data['user_address_name'] =Q('post.order_receiver');
			$data['user_address_tel'] =Q('post.tel');
			$data['user_address_phone'] =Q('post.phone');
			$data['user_address_all'] =Q('post.order_address');
			$data['user_address_place'] =Q('post.place');			
			$data['user_uid']= $uid;
			//写入数据库
			$id = M('user_address')->add($data);
			//返回地址id
			$this->ajax($id);
		}
	}
	
	//提交订单
	public function makeOrder(){
		if(IS_AJAX){
			$payway = Q('post.payway');
			$uid = $_SESSION['uid'];
			$addressId = Q('post.sendaddress');
			//查询用户地址信息
			$addressInfo = M('user_address')->where("user_uid ='{$uid}'  and address_id ='{$addressId}'")->find();
			//重组
			$data['user_uid'] = $uid; //用户id
			$data['order_nums'] = '8'. date('ymdhis').mt_rand(0, 1000) .'8' .mt_rand(0, 100) ;  //订单号
			$data['order_receiver'] =$addressInfo['user_address_name'];
			$data['order_address'] =$addressInfo['user_address_all'];
			$data['order_user_tel'] =$addressInfo['user_address_tel'];
			$data['order_user_place'] =$addressInfo['user_address_place'];
			$data['order_phone'] =$addressInfo['user_address_phone'];
			$data['order_addTime'] = time();
			$data['order_payway'] = $payway;
			$data['order_amount'] = $_SESSION['cart']['total'];
			//创建订单
			$orderId = M('user_order')->add($data);
			//购物车信息 写入订单列表
			$cartGoods = Cart::getAllData();
			$data = array();
			foreach($cartGoods['goods'] as $k => $v ){
				$data['order_list_nums'] = $v['num'];
				$data['order_list_subtotal'] = $v['total'];
				$data['goods_gid'] = $v['id'];
				$data['order_id'] = $orderId;
				M('order_list')->add($data);
			}
			//把订单写入session
			$_SESSION['orderId'] = $orderId;
			$data['oid'] = $orderId;
			$this->ajax($data);			
		}
	}

	//订单支付
	public function payfoOrder(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$orderId = Q('post.oid');
			//查询订单支付方式 执行对应方法
			$order = M('user_order')->where("oid='{$orderId}'")->find();
			if($order['order_payway'] == 0){ //账户余额支付
				//扣除用户款项
				$userInfo = M('user')->where("uid='{$uid}'")->find();
				$data['money'] = (int)$userInfo['money'] - (int)$order['order_amount'] ;
				if($data['money']<0){
					//余额不足
					$data['error']='0';
					$this->ajax($data);return false;
				}
				M("user")->where("uid = '{$uid}' ")->update($data);
				//修改订单支付状态
				$data['order_status'] = 1;
				M('user_order')->where("oid='{$orderId}'")->update($data);
				//读取商品表 把商品数量减少
				
				//清空购物车
				unset($_SESSION['cart']);
				$data['error']='1';
				$data['oid'] =$orderId;
				$this->ajax($data);
			} 
		}
	}
	

	
 }

 
